package com.efounder.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.NonNull;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.imageloader.GlideImageLoader;
import com.efounder.mobilemanager.R;
import com.utilcode.util.SizeUtils;

/**
 * 应用内截屏显示图片的dialog(仿淘宝)
 * 截屏的图片可以分享给好友
 * 自定义dialog https://www.jianshu.com/p/b48da813acf5
 *
 * @autor yqs
 * @date 2018/11/5 13:34
 **/
public class ScreenShotDialog extends Dialog {
    private static final String TAG = "ScreenShotDialog";
    private Context mContext;
    private String screenShotImagePath;

    private View rootView;
    private ImageView ivImage;
    private ImageView ivShare;
    private TextView tvShareToFriend;
    private ShareClickListener listener;

    private float mDownX;
    private float currentX;

    public ScreenShotDialog(@NonNull Context context, String screenShotImagePath) {
        super(context, R.style.ScreenShotDialog);
        this.mContext = context;
        this.screenShotImagePath = screenShotImagePath;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mobile_manager_dialog_screen_shot);
        initView();
        initLlistener();
        initData();

        Window window = this.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.x = SizeUtils.dp2px(12);
        params.y = SizeUtils.dp2px(0);
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
        //设置进入动画
        window.setWindowAnimations(R.style.ScreenShotDialogAnim);


    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        rootView = findViewById(R.id.fl_layout);
        ivImage = (ImageView) findViewById(R.id.iv_image);
        ivShare = (ImageView) findViewById(R.id.iv_share);
        tvShareToFriend = (TextView) findViewById(R.id.tv_share_to_friend);
        rootView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mDownX = event.getX();
                        break;

                    case MotionEvent.ACTION_MOVE:
                        currentX = event.getX();
                        float currentRawX = event.getRawX();
                        float currentRawY = event.getRawY();
                        break;

                    case MotionEvent.ACTION_UP:
                        if (currentX - mDownX > 80) {
                            //向右滑动
                            dismiss();
                            return true;
                        } else {
                            return false;
                        }

                }
                return false;

            }
        });
    }

    private void initLlistener() {
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    dismiss();
                    listener.onShareClick(screenShotImagePath);
                }
            }
        });
    }

    private void initData() {
        GlideImageLoader.getInstance().displayImage(mContext, ivImage, screenShotImagePath);
    }

    @Override
    public void show() {
        try {
            super.show();
            //两秒后取消显示
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    //当activity存在并且弹窗在show状态的时候
                    if(mContext!=null&&!((Activity)mContext).isDestroyed()&&isShowing()){
                        ((Activity)mContext).runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dismiss();
                            }
                        });

                    }

                }
            }, 3500);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public interface ShareClickListener {
        void onShareClick(String imagePath);
    }

    public void setOnShareClickListener(ShareClickListener listener) {
        this.listener = listener;
    }

}
